package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.merchant.UserJpushEntity;
import com.dragon.modules.yuegou.enums.merchant.UserJpushType;
import com.dragon.modules.yuegou.mapper.merchant.UserJpushMapper;
import com.dragon.modules.yuegou.model.merchant.User;
import com.dragon.modules.yuegou.model.merchant.UserJpush;
import com.dragon.modules.yuegou.query.merchant.UserJpushQuery;
import com.dragon.modules.yuegou.service.merchant.UserJpushService;
import com.dragon.modules.yuegou.service.merchant.UserService;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商户用户极光对应关系表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-12-26 17:08:02
 * @version 2.0
 */
@Service("merUserJpushService")
public class UserJpushServiceImpl
		extends ServiceImpl<UserJpushMapper, UserJpushEntity>
		implements UserJpushService {

	@Autowired
	private UserService userService;

	@Override
	public void createJpush(String userId, String jpushRegId,Integer type) {
		Check.notNull(userId,"用户id不能为空");
		Check.notNull(jpushRegId,"极光id不能为空");
		
		User user = userService.getBySysUserId(userId);

		//判断是否存在
		UserJpushQuery query = new UserJpushQuery();
		query.setJpushId(jpushRegId);
		query.setMerchantId(user.getMerchantId());
		query.setMerchantUserId(userId);
		query.setType(UserJpushType.from(type));
		query.setStatus(false);
		if (count(query.toQueryWrapper())==0){
			UserJpush userJpush = new UserJpush();
			userJpush.setJpushId(jpushRegId);
			userJpush.setMerchantId(user.getMerchantId());
			userJpush.setMerchantUserId(userId);
			userJpush.setType(UserJpushType.from(type));
			userJpush.setStatus(false);

			save(userJpush);
		}

	}

	@Override
	public void deleteJpush(String userId,String jpushRegId) {
		Check.notNull(userId,"用户id不能为空");
		Check.notNull(jpushRegId,"极光id不能为空");
		User user = userService.getBySysUserId(userId);

		//判断是否存在
		UserJpushQuery query = new UserJpushQuery();
		query.setJpushId(jpushRegId);
		query.setMerchantId(user.getMerchantId());

		List<UserJpushEntity> userJpushList = list(query.toQueryWrapper());
		userJpushList.stream().forEach(userJpush -> {
			userJpush.setStatus(true);
			updateById(userJpush);
		});
	}

	@Override
	public List<UserJpush> getUserJpushList(UserJpushQuery query) {
		return baseMapper.getUserJpushList(query);
	}
}